package io.gitee.thghh.chronicon.swing;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import io.gitee.thghh.chronicon.swing.theme.LightTheme;
import io.gitee.thghh.chronicon.swing.view.MainFrame;
import io.gitee.thghh.generate.core.CodeApplication;
import liquibase.analytics.configuration.AnalyticsArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.jdesktop.core.animation.timing.Animator;
import org.jdesktop.core.animation.timing.TimingSource;
import org.jdesktop.swing.animation.timing.sources.SwingTimerTimingSource;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import javax.swing.*;

/**
 * 代码生成器
 *
 * @author pengzhikang
 * @date 2025/3/19 15:20
 * @since 1.0.0
 */
@Slf4j
@SpringBootApplication
@EnableEncryptableProperties
public class ChroniconCodeGenerateApp {

    public static void main(String[] args) {
        try {
            // disable sending product usage data and analytics to Liquibase
            FieldUtils.writeDeclaredField(AnalyticsArgs.ENABLED, "defaultValue", Boolean.FALSE, true);

            SpringApplicationBuilder builder = new SpringApplicationBuilder(ChroniconCodeGenerateApp.class);
            //设置headless=false，设置web为none
            ConfigurableApplicationContext context = builder.headless(false).web(WebApplicationType.NONE).run(args);

            ConfigurableEnvironment environment = context.getEnvironment();
            String applicationName = environment.getProperty("spring.application.name");

            System.out.println(applicationName + " start");

            SwingUtilities.invokeLater(() -> {
                try {
                    TimingSource ts = new SwingTimerTimingSource();
                    Animator.setDefaultTimingSource(ts);
                    ts.init();
                    UIManager.setLookAndFeel(LightTheme.class.getName());
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }


                MainFrame frame = MainFrame.getInstance();
                frame.adjustUI();
                frame.adjustStyle();
            });
        } catch (Exception e) {
            log.error("程序启动失败", e);
        }
    }
}